package org.makcro.sms.services.chat.base.senders;

import java.util.Date;

import org.makcro.sms.services.chat.base.dao.AnswerDao;
import org.makcro.sms.services.chat.model.Answer;
import org.makcro.sms.services.chat.model.Question;

import com.klikmee.sms.cef.CoreAdapter;

/**
 * <p>
 * Base class for {@link MessageSender} implementations. Exposes
 * {@link CoreAdapter} and {@link AnswerDao} instances and
 * {@link #persistAnswer(Question, String, Long, boolean)} method
 * </p>
 * 
 * @author makcro
 * 
 */
public abstract class AbstractMessageSender implements MessageSender {

	/**
	 * {@link CoreAdapter} instance used to send messages via core platform.
	 */
	protected CoreAdapter	coreAdapter	= null;
	protected AnswerDao		answerDAO	= null;

	public AbstractMessageSender() {
		super();
	}

	public void setCoreAdapter(CoreAdapter coreAdapter) {
		this.coreAdapter = coreAdapter;
	}

	public void setAnswerDAO(AnswerDao answerDAO) {
		this.answerDAO = answerDAO;
	}

	/**
	 * Persists {@link Answer} object into database
	 * 
	 * @param question
	 * @param messageText
	 * @param refCoreId
	 * @param visible
	 *            TODO
	 */
	protected void persistAnswer(Question question, String messageText,
			Long refCoreId, boolean visible) {
		Answer answer = new Answer();
		answer.setAnswer( messageText );
		answer.setCoreID( refCoreId );
		answer.setDateAnswered( new Date() );
		answer.setFromNumber( question.getToNumber() );
		answer.setQuestion( question );
		answer.setToNumber( question.getFromNumber() );
		answer.setVisable( visible );
		// question has already been persisted, so call edit to avoid cascade
		// persist on question
		this.answerDAO.edit( answer );
	}

	public abstract Long sendMessage(String messageText, Long coreReferenceId, Object data);

}